use std::fmt::{self, Display, Formatter, Write};

use serde::Serialize;

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct GeoIpItem {
    pub region: String,
    pub province: Option<String>,
}

impl Display for GeoIpItem {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.region)?;
        if let Some(province) = &self.province {
            f.write_char('@')?;
            f.write_str(province)?;
        }
        Ok(())
    }
}

impl Serialize for GeoIpItem {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}
